﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSL.Mode;
using TSL.Common;

namespace TSL.Web.UI
{
    public class BasePage : System.Web.UI.Page
    {

        private TSL.Model.webconfig _webconfig = null;
        public TSL.Model.webconfig WebConfig
        {
            get
            {
                if (_webconfig == null)
                {
                    _webconfig = new TSL.BLL.webconfig().GetModelByCache();
                }
                return _webconfig;
            }
            set
            {
                _webconfig = value;
            }
        }

        public string WebSiteName()
        {
            if (WebConfig != null && WebConfig.URL != String.Empty)
            {
                return WebConfig.name;
            }
            else
            {
                return "UK";
            }
        }

        public TSL.Model.caipiao _CaiPiao = null;
        public TSL.Model.caipiao CaiPiao
        {
            get
            {
                if (Request["id"] == null)
                {
                    return null;
                }
                try
                {
                    int ssc_idInt = int.Parse(Request.QueryString["id"].ToString());
                    if (_CaiPiao == null)
                    {
                        _CaiPiao = new TSL.BLL.caipiao().GetModelByCache(ssc_idInt);
                    }
                    return _CaiPiao;
                }
                catch
                {
                }
                return null;
            }
        }

        /// <summary>
        /// 获取彩票ID
        /// </summary>
        public string GetCaiPiaoID()
        {
            if (CaiPiao == null)
            {
                return "100001";
            }
            else
            {
                return CaiPiao.id;
            }
        }

        /// <summary>
        /// 获取彩票名称
        /// </summary>
        public string SSC_Name()
        {
            if (CaiPiao != null)
            {
                return CaiPiao.sscname;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取彩票ID
        /// </summary>
        public int SSC_ID()
        {
            if (CaiPiao != null)
            {
                return int.Parse(CaiPiao.id);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 客服
        /// </summary>
        public string GetKeFu()
        {
            return Config.KeFu;
        }

        /// <summary>
        /// 网站根路径
        /// </summary>
        public string WebRoot()
        {
            if (!string.IsNullOrEmpty(Config.BindingDomain))
            {
                return Config.BindingDomain;
            }

            if (WebConfig != null && WebConfig.URL != String.Empty)
            {
                return WebConfig.URL;
            }
            else
            {
                return Page.ResolveUrl("~/");
            }
        }

        /// <summary>
        /// 全部css路径
        /// </summary>
        /// <returns></returns>
        public string Path()
        {
            string theme = "";

            if (!string.IsNullOrEmpty(Config.SkinTheme))
            {
                return WebRoot() + "theme/" + Config.SkinTheme + "/";
            }

            if (WebConfig != null && WebConfig.skintheme != String.Empty)
            {
                theme = WebRoot() + "theme/" + WebConfig.skintheme + "/";
            }
            else
            {
                theme = WebRoot() + "theme/default/";
            }

            return theme;
        }

        /// <summary>
        /// 路径，不包含 /
        /// </summary>
        /// <returns></returns>
        public string PathNotLin()
        {
            string str = Path();
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return str.Substring(0, str.Length - 1);
        }

        /// <summary>
        /// 返回图片目录路径
        /// </summary>
        /// <returns></returns>
        public string ImagePath()
        {
            if (!string.IsNullOrEmpty(Config.SkinTheme))
            {
                return WebRoot() + "theme/" + Config.SkinTheme + "/images/";
            }

            string imagepath = WebRoot() + "theme/default/images/";
            if (WebConfig != null && WebConfig.skintheme != String.Empty)
            {
                imagepath = WebRoot() + "theme/" + WebConfig.skintheme + "/images/";
            }
            else
                imagepath = WebRoot() + "theme/default/images/";
            return imagepath;
        }



        /// <summary>
        /// 检测当前用户是否登录 只适用于普通会员
        /// </summary>
        public bool IsLogin
        {
            get
            {
                bool login = false;
                if (Page.User.Identity.IsAuthenticated)
                    login = true;
                return login;
            }
        }

        /// <summary>
        /// 返回用户名 只适用于普通会员 如果没有登录 返回空
        /// </summary>
        public string UserName
        {
            get
            {
                string username = String.Empty;
                if (AsUser != null)
                {
                    username = AsUser.username;
                }
                return username;
            }
        }

        private TSL.Model.SYS_users _asuser = null;
        /// <summary>
        /// 返回当前登录的用户model
        /// </summary>
        public TSL.Model.SYS_users AsUser
        {
            get
            {
                if (_asuser == null)
                {
                    if (Page.User.Identity.IsAuthenticated)
                    {
                        TSL.Model.SYS_users oper = new TSL.BLL.SYS_users().GetModelByName(Page.User.Identity.Name);
                        if (oper != null)
                            _asuser = oper;
                    }
                }
                return _asuser;
            }
        }
        /// <summary>
        /// 计算返奖模式
        /// </summary>
        public int FJMode(int fdnum)
        {
            if (WebConfig != null)
            {
                return (WebConfig.FatFJ + (fdnum * int.Parse(WebConfig.kedumax.ToString())));
            }
            return 0;
        }

        public void isLogin()
        {
            if (!nullResults.Results)
            {
                Response.Write("当前系统未注册！");
                Response.End();
            }
        }
    }
}
